package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("DetectdepartmentController")
public class DetectdepartmentController extends CmiEmaController {

	@RequestMapping("/listDetectdepartments")
	public ModelAndView listDetectdepartments(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = detectdepartmentPaginationService.updatePagination(request, pagination);
		Set<Detectdepartment> detectdepartments = detectdepartmentPaginationService
				.getPaginationDetectdepartments(pagination);

		mav.addObject("detectdepartments", detectdepartments);
		mav.addObject("entityName", "detectdepartment");
		mav.addObject("pagination", pagination);
		mav.setViewName("detectdepartment/listDetectdepartments.jsp");
		return mav;
	}

	public String listDetectdepartments() {
		return "redirect:/listDetectdepartments";
	}

	@RequestMapping("/viewDetectdepartment")
	public ModelAndView viewDetectdepartment(@RequestParam Integer detectdepartmentId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("detectdepartment", detectdepartmentDAO.findDetectdepartmentByPrimaryKey(detectdepartmentId));
		mav.setViewName("detectdepartment/viewDetectdepartment.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteDetectdepartments")
	public ModelAndView confirmDeleteDetectdepartments(
			@RequestParam(value = "deleteDetectdepartmentsIds", required = true) Set<Integer> deleteDetectdepartmentsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Detectdepartment> deleteDetectdepartments = detectdepartmentDAO
				.findDetectdepartmentsByIds(deleteDetectdepartmentsIds);
		Set<Detectdepartment> referencedDetectdepartments = detectdepartmentService
				.loadReferenceDetectdepartments(deleteDetectdepartments);
		boolean hasReferenced;
		if (referencedDetectdepartments.size() == 0) {
			mav.addObject("deleteEntitys", deleteDetectdepartments);
			hasReferenced = false;
		} else {
			deleteDetectdepartments.removeAll(referencedDetectdepartments);
			mav.addObject("referencedEntitys", referencedDetectdepartments);
			mav.addObject("deleteEntitys", deleteDetectdepartments);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Detectdepartment");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listDetectdepartments");
		return mav;
	}

	@RequestMapping("/deleteDetectdepartment")
	public String deleteDetectdepartment(@RequestParam Integer detectdepartmentId) {
		Detectdepartment detectdepartment = detectdepartmentDAO.findDetectdepartmentByPrimaryKey(detectdepartmentId);
		detectdepartmentService.deleteDetectdepartment(detectdepartment);
		return "forward:/listDetectdepartments";
	}

	@RequestMapping("/confirmDeleteDetectdepartment")
	public ModelAndView confirmDeleteDetectdepartment(@RequestParam Integer detectdepartmentId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("detectdepartment", detectdepartmentDAO.findDetectdepartmentByPrimaryKey(detectdepartmentId));

		mav.setViewName("detectdepartment/deleteDetectdepartment.jsp");

		return mav;
	}

	@RequestMapping("/deleteDetectdepartments")
	public String deleteDetectdepartments(
			@RequestParam(value = "deleteDetectdepartmentsId", required = true) Set<Integer> deleteDetectdepartmentsId) {
		detectdepartmentService.deleteDetectdepartments(deleteDetectdepartmentsId);
		return "forward:/listDetectdepartments";
	}

	@RequestMapping("/editDetectdepartment")
	public ModelAndView editDetectdepartment(@RequestParam Integer detectdepartmentId) {

		ModelAndView mav = new ModelAndView();

		Detectdepartment detectdepartment = detectdepartmentDAO.findDetectdepartmentByPrimaryKey(detectdepartmentId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("detectdepartment", detectdepartment);

		// 把所有的引用detectdepartment对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectdepartmentService.loadDetectdepartmentObjectReferencedSet(detectdepartment));

		// 将编辑页面detectdepartment添加到mav中
		mav.setViewName("detectdepartment/editDetectdepartment.jsp");
		return mav;
	}

	@RequestMapping("/newDetectdepartment")
	public ModelAndView newDetectdepartment() {
		ModelAndView mav = new ModelAndView();
		Detectdepartment detectdepartment = new Detectdepartment();

		mav.addObject("detectdepartment", detectdepartment);

		// 把所有的引用detectdepartment对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectdepartmentService.loadDetectdepartmentObjectReferencedSet(detectdepartment));

		mav.addObject("newFlag", true);
		mav.setViewName("detectdepartment/editDetectdepartment.jsp");

		return mav;
	}

	@RequestMapping("/saveDetectdepartment")
	public ModelAndView saveDetectdepartment(@ModelAttribute Detectdepartment detectdepartment) {

		ModelAndView mav = new ModelAndView();

		Address addressTemp = detectdepartment.getAddress();
		Address address = addressTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(addressDAO.findAddressById(addressTemp.getId()));

		Detectdepartment existingDetectdepartment = CmiHibernateProxyUtil.deproxy(detectdepartmentDAO
				.findDetectdepartmentByPhysicalPrimaryKey(detectdepartment.getName(), address.getId()));

		detectdepartment.setAddress(address);
		// 新建记录
		if (detectdepartment.getId() == null) {
			if (detectdepartment.equals(existingDetectdepartment)) {

				mav.addAllObjects(detectdepartmentService.loadDetectdepartmentClassReferencedSet(detectdepartment));
				// mav = editContinue(mav, detectdepartment);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectdepartment/editDetectdepartment.jsp");
			} else {
				detectdepartmentService.saveDetectdepartment(detectdepartment);
				mav.setViewName("forward:/listDetectdepartments");
			}
		}
		// 编辑记录
		else {
			Detectdepartment originDetectdepartment = CmiHibernateProxyUtil
					.deproxy(detectdepartmentDAO.findDetectdepartmentByPrimaryKey(detectdepartment.getId()));
			if (!detectdepartment.equals(originDetectdepartment) && detectdepartment.equals(existingDetectdepartment)) {
				mav.addAllObjects(detectdepartmentService.loadDetectdepartmentClassReferencedSet(detectdepartment));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectdepartment/editDetectdepartment.jsp");
			} else {
				detectdepartmentService.saveDetectdepartment(detectdepartment);
				mav.setViewName("forward:/listDetectdepartments");
			}
		}
		return mav;
	}

	@RequestMapping("/detectdepartmentController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
